package kr.hkit.summary_java.serviceui;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Set;

import javax.swing.BoxLayout;

import kr.hkit.myframework.uicomp.Emp_InputPanel;
import kr.hkit.summary_java.dao.DaoInterface;
import kr.hkit.summary_java.dao.EmployeeDAO;
import kr.hkit.summary_java.erp.dto.Employee;

import javax.swing.JButton;

import kr.hkit.myframework.uicomp.InputPanel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Panel_EmployeeUITest extends JPanel implements ActionListener, FocusListener {

	private String btnText;
	private EmployeeDAO dao;
	private InputPanel pEmpNo;
	private InputPanel pEmpName;
	private Emp_InputPanel pTitle;
	private Emp_InputPanel pManager;
	private InputPanel pSalary;
	private Emp_InputPanel pDno;
	private JPanel panel;
	private JPanel panel_South;
	private JButton btnDynamic;
	
	private HashMap<String, Integer> hashDepts;
	private HashMap<String, Integer> hashTitles;
	private HashMap<String, Integer> hashMgn;
	/**
	 * Create the panel.
	 */

	public Panel_EmployeeUITest(String txt, EmployeeDAO dao) {
		super();
		this.btnText = txt;
		this.dao = dao;
		initialize();
	}

	private void initialize() {
		setLayout(new BorderLayout(0, 0));

		panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		panel.setLayout(new GridLayout(0, 1, 0, 0));

		pEmpNo = new InputPanel("사원번호");
		panel.add(pEmpNo);
		pEmpNo.getTf().addFocusListener(this);
		
		pEmpName = new InputPanel("사원명");
		panel.add(pEmpName);

		pTitle = new Emp_InputPanel("직책");
		try{
			hashTitles = dao.selectedTitleList();
			Set<String> s = hashTitles.keySet();
			String[] titleItems = new String[hashTitles.size()];
			s.toArray(titleItems);
			pTitle.addItems(titleItems);
		} catch(SQLException e2){
			e2.printStackTrace();
		}
		panel.add(pTitle);

		pManager = new Emp_InputPanel("상급자");
		try {
			hashMgn =dao.selectedManager();
			Set<String> arr= hashMgn.keySet();
			String[] mgnItems = new String[hashMgn.size()];
			arr.toArray(mgnItems);
			pManager.addItems(mgnItems);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		panel.add(pManager);

		pSalary = new InputPanel("급여");
		panel.add(pSalary);

		pDno = new Emp_InputPanel("부서명");
		try {
			hashDepts = dao.selectedDeptList();
			Set<String> arr= hashDepts.keySet();
			String[] deptItems = new String[hashDepts.size()];
			arr.toArray(deptItems);
			pDno.addItems(deptItems);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		panel.add(pDno);

		panel_South = new JPanel();
		add(panel_South, BorderLayout.SOUTH);
		panel_South.setLayout(new GridLayout(1, 0, 0, 0));

		
		if(!btnText.equals("추가")){
			panelDisable();
		}
		btnDynamic = new JButton(btnText);
		if(btnText.equals("검색")){
			panelDisable();
		}
		btnDynamic.addActionListener(this);
		panel_South.add(btnDynamic);
	}

	private void panelDisable() {
		pEmpNo.getTf().setEnabled(true);
		pEmpName.getTf().setEnabled(false);
		pTitle.getCmb().setEnabled(false);
		pManager.getCmb().setEnabled(false);
		pSalary.getTf().setEnabled(false);
		pDno.getCmb().setEnabled(false);
	}
	
	private void panelEnable(){
		pEmpNo.getTf().setEnabled(false);
		pEmpName.getTf().setEnabled(true);
		pTitle.getCmb().setEnabled(true);
		pManager.getCmb().setEnabled(true);
		pSalary.getTf().setEnabled(true);
		pDno.getCmb().setEnabled(true);
	}
	
	private void panelClear(){
		pEmpNo.setTfTxt("");
		pEmpName.setTfTxt("");
		pTitle.setDefaultSelectIndex(0);
		pManager.setDefaultSelectIndex(0);
		pSalary.setTfTxt("");
		pDno.setDefaultSelectIndex(0);
	}
	
	private Employee getDept(){
		return new Employee(
				Integer.parseInt(pEmpNo.getTf().getText().trim()), 
				pEmpName.getTf().getText(), 
				hashTitles.get(pTitle.getSelectItem()==null?"":pTitle.getSelectItem()), 
				hashMgn.get(pManager.getSelectItem()==null?"":pManager.getSelectItem()), 
				Integer.parseInt(pSalary.getTf().getText().trim()), 
				hashDepts.get(pDno.getSelectItem()==null?"":pDno.getSelectItem())
				);
	}
	

	public void actionPerformed(ActionEvent e) {
		/*if (e.getSource() == btnDynamic) {
			actionPerformedBtnDynamic(e);
		}*/
		if(e.getActionCommand().equals("추가")){
			actionPerformedBtnAdd(e);
		}else if(e.getActionCommand().equals("삭제")){
			actionPerformedBtnDel(e);
		}else if(e.getActionCommand().equals("수정")){
			actionPerformedBtnUpdate(e);
		}else if(e.getActionCommand().equals("검색")){
			actionPerformedBtnSearch(e);
		}
	}
	private void actionPerformedBtnSearch(ActionEvent e) {
		searchItem();
		
	}

	private void actionPerformedBtnUpdate(ActionEvent e) {
		Employee emp = getDept();
		try {
			dao.updateItemByNo(emp);
			JOptionPane.showMessageDialog(this,"수정되었습니다");
			panelDisable();
			panelClear();
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this,"수정실패 다시 확인하세요.");
		}		
		
	}

	private void actionPerformedBtnDel(ActionEvent e) {
		Employee emp = getDept();
		try {
			dao.deleteItemByNo(emp);
			JOptionPane.showMessageDialog(this,"삭제되었습니다");
			panelDisable();
			panelClear();
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this,e1);
		}
		
	}

	private void actionPerformedBtnAdd(ActionEvent e) {
		Employee emp = getDept();
		try {
			dao.insertItem(emp);
			JOptionPane.showMessageDialog(this,"입력되었습니다");
			panelClear();
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this,"입력실패 다시 확인하세요.");
		}
		
	}

	@Override
	public void focusGained(FocusEvent e) {
	}

	@Override
	public void focusLost(FocusEvent e) {
		searchItem();
		if (btnDynamic.getText().equals("수정")){
			panelEnable();
		}
	}

	private void searchItem() {
		DecimalFormat df = new DecimalFormat("#,###");
		int no = Integer.parseInt(pEmpNo.getTf().getText().trim());
		Employee d = new Employee();
		d.setEmpNo(no);
		try {
			d = dao.searchItem(d);
			pEmpName.setTfTxt(d.getEmpName());
			pTitle.setSelctItem(dao.selectedTitleById(d.getTitle()));
			pManager.setSelctItem(dao.selectedNameByEmpNo(d.getManager()));
			pSalary.setTfTxt(df.format(d.getSalary()));
			pDno.setSelctItem(dao.selectedDeptNameByDeptno(d.getDeptno()));
		} catch (SQLException e1) {
			e1.printStackTrace();
		} catch (NullPointerException e1){
			if (btnDynamic.getText().equals("추가")){
				JOptionPane.showMessageDialog(this, "추가 가능");
			}else{
				JOptionPane.showMessageDialog(this, "존재하지 않음");
				pEmpNo.setTfTxt("");
			}
			
		}
		
	}
}
